package edu.polsl.bio.str.manager.messaging;

import edu.polsl.bio.str.common.messaging.StrJobPayload;
import edu.polsl.bio.str.manager.command.Command;
import edu.polsl.bio.str.manager.constant.Constants;
import edu.polsl.bio.str.manager.event.BaseManagerEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.message.GenericMessage;

import java.util.HashMap;
import java.util.Map;

/**
 * //todo class description
 * <p/>
 * Created on: Jul 12, 2011 2:05:14 PM <br/>
 * <a href="http://www.polsl.pl">www.polsl.pl</a>
 *
 * @author lukasz.olczak@polsl.pl
 */
public class ManagerMessagePublisherImpl implements ManagerMessagePublisher {
  /**
   * slf4j Logger.
   */
  private static final Logger log = LoggerFactory.getLogger(ManagerMessagePublisherImpl.class.getName());

  private MessageChannel eventChannel;

  private MessageChannel loaderChannel;

  private MessageChannel dispatcherChannel;

  @Override
  public void publishEvent(BaseManagerEvent event) {
    log.debug("Publishing {}", event);
    GenericMessage<BaseManagerEvent> messageEvent = new GenericMessage(event);
    eventChannel.send(messageEvent);
  }

  @Override
  public void publishLoaderCommand(Command cmd) {
    log.debug("Publishing loader message {}", cmd);
    loaderChannel.send(new GenericMessage(cmd));
  }

  @Override
  public void publishDispatcherPayload(String nodeIp, StrJobPayload payload) {
    log.debug("Publishing job payload {} for node {}", payload, nodeIp);
    Map<String, Object> headers = new HashMap();
    headers.put(Constants.NODE_IP_HEADER, nodeIp);
    GenericMessage<StrJobPayload> msg = new GenericMessage(payload, headers);
    dispatcherChannel.send(msg);
  }

  public void setEventChannel(MessageChannel eventChannel) {
    this.eventChannel = eventChannel;
  }

  public void setLoaderChannel(MessageChannel loaderChannel) {
    this.loaderChannel = loaderChannel;
  }

  public void setDispatcherChannel(MessageChannel dispatcherChannel) {
    this.dispatcherChannel = dispatcherChannel;
  }
}
